//
//  AppDelegate.swift
//  MyMicroBlog
//
//  Created by H on 15/8/1.
//  Copyright © 2015年 H. All rights reserved.
//

import UIKit
/// 视图控制器切换通知字符串
let HMRootViewControllerSwitchNotification = "HMRootViewControllerSwitchNotification"
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        print(UserAccount.sharedUserAccount)
        //注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchViewController:", name: HMRootViewControllerSwitchNotification, object: nil)

        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultViewController()
//        window?.rootViewController = NewFeatureViewController()
        window?.makeKeyAndVisible()
        
        
        return true
    }
    deinit {
        // 注销通知 - 只是一个习惯
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    /// 切换控制器，记住：一定在 AppDelegate 中统一修改！
    func switchViewController(n: NSNotification) {
        let mainVC = n.object as! Bool
        
        window?.rootViewController = mainVC ? MainViewController() : WelcomeViewController()
    }

    //启动部分控制器切换
    
    private func defaultViewController()->UIViewController {
        if !UserAccount.userLogon {
        
        return MainViewController()
        }
        return isNewUpdate() ? NewFeatureViewController() : WelcomeViewController()
    
    
    }
    
    

 //检查新版本
    private  func isNewUpdate()->Bool {
    
    let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        let sandboxVersionKey = "sandboxVersionKey"
        let  sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        return currentVersion > sandboxVersion
    
    }


}

